#include "globalVariables.h"
#include "DataDefine.h"

Card* getCard(int id);
int deckSort(list<Card*>& dst);

int deckeditLeftButtonDown()
{
	//card select
	if (!isSceneChanging)
	{
		//card select
		if (mouseX >= SELECT_X && mouseX <= SELECT_X + SELECT_WIDTH
			&& mouseY >= SELECT_Y && mouseY <= SELECT_Y + SELECT_HEIGHT)
		{
			selectX = (mouseX - SELECT_X) / (CARD_WIDTH_SMALL+SELECT_GAP);
			selectY = (mouseY - SELECT_Y) / (CARD_HEIGHT_SMALL + SELECT_GAP);
			//check if is selected
			if ((mouseX-selectX*(CARD_WIDTH_SMALL+SELECT_GAP)-SELECT_X<=CARD_WIDTH_SMALL) 
				&& (mouseY-selectY*(CARD_HEIGHT_SMALL+SELECT_GAP)-SELECT_Y<=CARD_HEIGHT_SMALL))
			{
				if (selectedShownCard && selectedShownCard->id == (*cardptr)[selectY][selectX]->id)
				{
					cardSelectBuffer = true;
				}
				//change showing card
				if (selectedShownCard)
				{
					delete selectedShownCard;
				}
				selectedShownCard = getCard((*cardptr)[selectY][selectX]->id);
			}
		}
		//deck select
		if (mouseX >= SELECT_TITLE_X && mouseX <= SELECT_TITLE_X + SELECT_TITLE_WIDTH
			&& mouseY >= SELECT_TITLE_Y && mouseY <= SELECT_TITLE_Y + SELECT_TITLE_HEIGHT*editingDeck.size())
		{
			selectX = 0;
			selectY = (mouseY - SELECT_TITLE_Y) / SELECT_TITLE_HEIGHT;
			deckSelectBuffer = true;
		}
	}

	return 0;
}

int deckeditLeftButtonUp()
{
	//card
	if (cardSelectBuffer)
	{
		cardSelectBuffer = false;
		if (editingDeck.size() < DECK_NUM)
		{
			editingDeck.push_back(getCard((*cardptr)[selectY][selectX]->id));
			deckSort(editingDeck);
		}
	}
	//deck
	if (deckSelectBuffer)
	{
		deckSelectBuffer = false;
		auto deckiter = editingDeck.begin();
		for (int i = 0; i < selectY; i++)
		{
			deckiter++;
		}
		delete (*deckiter);
		editingDeck.erase(deckiter);
	}

	return 0;
}